package com.df.tv.launcher.util;

import android.database.Cursor;
import android.net.Uri;
import android.text.TextUtils;

import com.df.tv.launcher.LauncherApplication;
import com.df.tv.launcher.params.AppTest;

public class SystemInfo {

    public static class NoSystemInfoException extends Exception {

        private static final long serialVersionUID = 1L;

        public NoSystemInfoException(String msg) {
            super(msg);
        }
    }

    private static final String AUTHORITY = "com.telecom.tv189.tm.sysinfo";
    private static final String SYSTEM_INFO = "system_info";
    private static final String SYSTEM_INFO_SN = "sn";
    private static final String SYSTEM_INFO_MODEL = "model";
    private static final String SYSTEM_INFO_MANUFACTURER = "manufacturer";
    private static final String SYSTEM_INFO_VERSION = "version";

    private static final String[] COLUMN_KEY = new String[] { SYSTEM_INFO_SN, SYSTEM_INFO_MODEL,
            SYSTEM_INFO_MANUFACTURER, SYSTEM_INFO_VERSION };

    public static SysInfo read() throws NoSystemInfoException {
        return newInfo(getProps());
        //return readTest();
    }

    private static SysInfo readTest() {
        String[] props = new String[] { AppTest.TEST_SN, AppTest.TEST_MODEL, AppTest.TEST_MANUFACTURER,
                AppTest.TEST_VERSION };
        return newInfo(props);
    }

    private static String[] getProps() throws NoSystemInfoException {
        String[] props = new String[4];
        Uri uri = Uri.parse("content://" + AUTHORITY + "/" + SYSTEM_INFO);
        Cursor cursor = LauncherApplication.getInstance().getContentResolver().query(uri, null, null, null, null);
        if (cursor != null) {
            cursor.moveToNext();
            for (int i = 0; i < props.length; i++) {
                int index = cursor.getColumnIndex(COLUMN_KEY[i]);
                props[i] = cursor.getString(index);
                if (TextUtils.isEmpty(props[i]))
                    throw new NoSystemInfoException(COLUMN_KEY[i]);
            }
            cursor.close();
        }
        return props;
    }

    private static SysInfo newInfo(String[] props) {
        SysInfo info = new SysInfo();
        info.setSn(props[0]);
        info.setModel(props[1]);
        info.setManufacturer(props[2]);
        info.setVersion(props[3]);
        info.setChannelId(null);
        return info;
    }

    public static class SysInfo {

        private String mSn;
        private String mModel;
        private String mManufacturer;
        private String mVersion;
        private String mChannelId;

        public String getSn() {
            return mSn;
        }

        private void setSn(String mSn) {
            this.mSn = mSn;
        }

        public String getModel() {
            return mModel;
        }

        private void setModel(String mMdel) {
            this.mModel = mMdel;
        }

        public String getManufacturer() {
            return mManufacturer;
        }

        private void setManufacturer(String mManufacturer) {
            this.mManufacturer = mManufacturer;
        }

        public String getVersion() {
            return mVersion;
        }

        private void setVersion(String mVersion) {
            this.mVersion = mVersion;
        }

        public String getChannelId() {
            return mChannelId;
        }

        private void setChannelId(String mChannelId) {
            this.mChannelId = mChannelId;
        }
    }
}
